# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=recode
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.6
pkgrel=5
pkgdesc="Converts files between various character sets and usages (mingw-w64)"
arch=('any')
url="http://recode.progiciels-bpi.ca/index.html"
license=('GPL' 'LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-gettext")
options=('!strip' 'staticlibs')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/pinard/recode/tarball/v${pkgver}"
        01-no-usr-share-info-dir-gz
        02-src-libiconv-c-utf8
        03-src-task-c-librecode
        04-src-hash-h-php4-collision
        05-src-request-c-pointer-recalculation
        06-libtool-update
        07-po-update
        08-all-linguas-update
        09-manpage-name-section
        10-src-recodext-h-gcc-fix
        11-src-names-c-memory-leak
        12-src-names-c-format-string
        99-config-guess-config-sub
        'recode-3.6-as-if.patch'
        'recode-3.6-gettextfix.diff'
        'autotools.patch'
        'mingw.patch')
sha256sums=('a9beecec22be42b1096de74d4dfb1051fa9cd252e4ae149d8aa6b5243d729e5e'
            'd3b625f7966bdb23596b85affafb3993694fc01cd68834f14681961448d92876'
            '68039c9a533b31941417e8a3f67c6c26c39b8e3c0dc2c265911ebf95b21e1a09'
            'd3527b0ab5be5897c05eab140109a7ff81c006c2fc992917f87a4008556ceb9b'
            '1af8a7725c3a7c6323045541ed8035f7d0092cfb1a90232c32f0a9221447b009'
            '51d0fb61ccfaf5f048a6a2f54bc9f583c5649f2ad34d988550484d9311c050d5'
            'b47a0656a6e0c1a3928e742947179ccdfbc48b3508a7a3c3eacf9c1e6e6a869d'
            '0b3d19580c032efcfd98c586372ad87ae6541addd1317595063d9097b56be870'
            '80222c6b0ad672299734eacd505b5de504af4250aa6c550f49ff536b1afc5b4c'
            '6b12b9bb2150212eb02c1e79de6aa47eea3f34d05fbfd0f791baf1e89d4d09b5'
            'b66055ef986d9368b2c970df9513f4d7522c9a7f4c7ee82545b8349e9df4c897'
            '4defe29ae1c7550ac4f54b26e1395449e8b2b6236c222e18b0f2a7d23152db70'
            '82aa2a7537e0c2defc54ebb49081730a5d9e059b1d9318fab87fc9f00df17741'
            'bdf656785d90b0da856765d5d689b032972c7ce539d9b3e499a3e047f6f1099c'
            '9ab5b40dd93461ca95c5f5dd197bbe0767c45fe64fb1898f073a24c5705822b5'
            'eb080ec09e508f24974d3bed2dc114d2d8c5d69615e98beea67aa07d4a1e076b'
            'd83b4303b58a5b9412b088bc749a2c68192410cee8297365ede9289c539c276d'
            'bf0a3a9302ba03c74ed53fd7ec10ecd91a89023e957f9a3d87c63790d3bb18a5')

prepare() {
  cd ${srcdir}/pinard-Recode-2127b34
  patch -Np1 -i ${srcdir}/recode-3.6-as-if.patch
  patch -Np1 -i ${srcdir}/recode-3.6-gettextfix.diff
  patch -Np1 -i ${srcdir}/01-no-usr-share-info-dir-gz
  patch -Np1 -i ${srcdir}/02-src-libiconv-c-utf8
  patch -Np1 -i ${srcdir}/03-src-task-c-librecode
  patch -Np1 -i ${srcdir}/04-src-hash-h-php4-collision
  patch -Np1 -i ${srcdir}/05-src-request-c-pointer-recalculation
  patch -Np1 -i ${srcdir}/06-libtool-update
  patch -Np1 -i ${srcdir}/07-po-update
  patch -Np1 -i ${srcdir}/08-all-linguas-update
  patch -Np1 -i ${srcdir}/09-manpage-name-section
  patch -Np1 -i ${srcdir}/10-src-recodext-h-gcc-fix
  patch -Np1 -i ${srcdir}/11-src-names-c-memory-leak
  patch -Np1 -i ${srcdir}/12-src-names-c-format-string
  patch -Np1 -i ${srcdir}/99-config-guess-config-sub
  patch -Np1 -i ${srcdir}/autotools.patch
  patch -Np1 -i ${srcdir}/mingw.patch
  sed -i '1i#include <stdlib.h>' src/argmatch.c
  sed -i -e '/^AM_C_PROTOTYPES/d' -e '/^AUTOMAKE_OPTIONS.*ansi2knr/d' \
  configure.in src/Makefile.am
  rm -f acinclude.m4
  autoreconf -fi
  libtoolize
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  export ac_cv_func_malloc_0_nonnull=yes
  export jm_cv_func_working_malloc=yes
  export jm_cv_func_working_realloc=yes
  ../pinard-Recode-2127b34/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --mandir=${MINGW_PREFIX}/share/man \
    --infodir=${MINGW_PREFIX}/share/info \
    --without-included-gettext

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install
  rm "${pkgdir}${MINGW_PREFIX}"/lib/charset.alias
}
